package com.practice;

import java.util.Iterator;
import java.util.function.Consumer;

/**
 * Created with IntelliJ IDEA.
 * Description:单向链表  addFirst
 * User: YJ
 * Date: 2023-9-02
 * Time: 15:16
 */
public class SingleLinkedList implements Iterable<Integer> {
    //头指针
    private Node head;

    /**
     * 迭代器遍历
     *
     * @return 返回迭代器对象
     */
    @Override
    public Iterator<Integer> iterator() {
        //匿名内部类
        return new Iterator<Integer>() {
            Node pointer = head;

            @Override
            public boolean hasNext() {  //是否有下一个元素
                return pointer != null;
            }

            @Override
            public Integer next() { //返回当前值，指向下一个元素
                int value = pointer.value;
                pointer = pointer.next;
                return value;
            }
        };
    }

    /**
     * 节点类
     */
    private static class Node {
        int value;
        Node next;

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }

    /**
     * 向链表头部添加
     *
     * @param value 要添加的节点的值
     */
    public void addFirst(int value) {
        //1.链表为空
        //head = new Node(value,null);
        //2.链表非空
        head = new Node(value, head);
    }

    /**
     * 遍历链表
     *
     * @param consumer 通过函数式接口实现
     */
    public void loop1(Consumer<Integer> consumer) {
        //定义一个变量指向链表的头
        Node pointer = head;
        //依次遍历链表
        while (pointer != null) {
            consumer.accept(pointer.value);
            pointer = pointer.next;
        }
    }

    /**
     * for循环遍历
     *
     * @param consumer 通过函数式接口实现
     */
    public void loop2(Consumer<Integer> consumer) {
        for (Node pointer = head; pointer != null; pointer = pointer.next) {
            consumer.accept(pointer.value);
        }
    }

    /**
     * 找最后一个节点
     *
     * @return 返回最后一个节点
     */
    private Node findLast() {
        if (head == null) {
            return null;
        }
        Node pointer = head;
        while (pointer.next != null) {
            pointer = pointer.next;
        }
        return pointer;
    }

    /**
     * 向链表尾部插入元素
     * @param value     要插入的值
     */
    public void addLast(int value) {
        Node newNode = new Node(value, null);
        //链表为空时
        if (head == null) {
            head = newNode;
            return;
        }
        Node last = findLast();
        last.next = newNode;
    }

    /*public void test1() {
        int i = 0;
        for (Node p = head;p != null;p = p.next,i++) {
            System.out.println(p.value + " 索引是：" + i);
        }
    }*/
    /*public void test2(int index) {
        int i = 1;
        for (Node p = head;p != null;p = p.next,i++) {
            if (i == index) {
                System.out.println(p.value);
            }
        }
    }*/
    /**
     * 根据索引查找节点
     *
     * @param index 索引值
     * @return 返回该索引对应的节点
     */
    private Node findNode(int index) {
        int i = 0;
        for (Node p = head; p != null; p = p.next, i++) {
            if (i == index) {
                return p;
            }
        }
        return null;
    }

    /**
     * 获取指定索引的节点的值
     *
     * @param index 要查找的索引
     * @return 返回该索引下节点的值
     */
    public int getValue(int index) {
        Node node = findNode(index);
        if (node == null) {
            throw getIllegalArgumentException(index);
        }
        return node.value;
    }

    /**
     * 向某一索引处添加元素
     * @param index     插入的索引
     * @param value     新节点的值
     */
    public void insert(int index, int value) {
        Node newNode = new Node(value, null);
        if (index == 0) {
            addFirst(value);
            return;
        }
        Node node = findNode(index);
        Node prev = findNode(index - 1);
        if (prev == null) {
            throw getIllegalArgumentException(index);
        }
        prev.next = newNode;
        newNode.next = node;
    }

    private IllegalArgumentException getIllegalArgumentException(int index) {
        return new IllegalArgumentException(
                String.format("index [%d] 不合法%n", index));
    }

    /**
     * 删除第一个节点
     */
    public void removeFirst() {
        if (head == null) {
            throw getIllegalArgumentException(0);
        }
        head = head.next;
    }

    /**
     * 删除索引位置对应的元素
     * @param index     索引
     */
    public void remove(int index) {
        if (index == 0) {
            removeFirst();
            return;
        }
        //找到该索引的上一个节点
        Node prev = findNode(index - 1);
        //找到该索引的节点
        Node node = findNode(index);
        if (head == null) {
            throw getIllegalArgumentException(0);
        }
        if (node == null) {
            throw getIllegalArgumentException(index);
        }
        if (prev == null) {
            throw getIllegalArgumentException(index);
        }
        prev.next = node.next;
    }
}

